package org.lex.ftp.ui.speed;

import java.awt.Color;
import java.awt.Component;

import javax.swing.JTable;
import javax.swing.table.DefaultTableCellRenderer;

import org.lex.net.log.ui.ColorConstants;
import org.lex.utils.text.SizeFormat;


public class SpeedTableRenderer extends DefaultTableCellRenderer {
	private Color goodBG = ColorConstants.REPLY_BG;
	private Color connectFailedBG = ColorConstants.ERROR_BG;
	private Color unkownColor = Color.white;
	private Color noSuchFileBG = ColorConstants.MISC_BG;
	private Color selectedFG = Color.white;
	private Color selectedBG = new Color(49, 106, 197);
	private Color unselectedFG = Color.black;
	private Color unselectedBG = null;

	@Override
	public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
		ServerStatus status = (ServerStatus) table.getValueAt(row, 1);
		// ================ set text ================
		String text = null;
		if (2 == column) {
			text = "";
			if (ServerStatus.Found == status)
				text = SizeFormat.autoFormat((Long) value) + "B";
		} else
			text = String.valueOf(value);
		this.setText(text);
		// ================ set color ================
		if (true == isSelected) {
			this.setForeground(selectedFG);
			this.setBackground(selectedBG);
		} else {
			switch (status) {
			case Unkown:
				unselectedBG = unkownColor;
				break;
			case ConnectFailed:
				unselectedBG = connectFailedBG;
				break;
			case NoSuchFile:
				unselectedBG = noSuchFileBG;
				break;
			case Found:
				unselectedBG = goodBG;
				break;
			}
			this.setForeground(unselectedFG);
			this.setBackground(unselectedBG);
		}
		return this;
	}
}
